/*
 * RatingList.java
 *
 * Represents a rating list.
 * A rating list may come from EGF or from FFG
 */

package gotha;

import java.io.*;
import java.util.*;

/**
 *
 * @author Luc Vannier
 */
public class RatingList {
    public static final int TYPE_UNDEFINED = 0;
    public static final int TYPE_EGF = 1;
    public static final int TYPE_FFG = 257;
    
    private int ratingListType = TYPE_UNDEFINED;
    private String strPublicationDate = "";
    private Vector<RatedPlayer> vPlayers = new Vector<RatedPlayer>();
    
    /** Creates a new instance of RatingList */
    public RatingList() {
        ratingListType = TYPE_UNDEFINED;
    }
    /** Creates a new instance of RatingList */
    public RatingList(int ratingListType, File f) {
        this.ratingListType = ratingListType;
        parseFile(f);
    }

    private void parseFile(File f){
        // Transfer file content to a Vector<String>
        Vector<String> vLines = new Vector<String>();
        try{
            FileInputStream fis = new FileInputStream(f);
            com.glaforge.i18n.io.SmartEncodingInputStream smartIS = new com.glaforge.i18n.io.SmartEncodingInputStream(fis);
            Reader reader = smartIS.getReader();
            BufferedReader d = new BufferedReader(reader);
 
            String s;
            do{
                 s = d.readLine();
                if (s != null){
                    vLines.add(s);
                }
            } while (s !=null);
            d.close();
            smartIS.close();
            fis.close();
        } catch (Exception e){
            e.printStackTrace();
        }
        
        // Parse rating list
        for (String strLine : vLines){
            if (strLine.length() == 0) continue;            
            int pos;
            
            if (ratingListType == TYPE_EGF){
                pos = strLine.indexOf("(");                
                if (pos >= 0) {
                    String str = strLine.substring(pos);
                    String[] dateElements = str.split(" ");
                    if ((dateElements.length >= 3)
                            && (dateElements[2].length() >= 2)
                            && (dateElements[2].substring(0,2).compareTo("20") == 0) ) // Just take care of the "2100 year bug" :)
                        strPublicationDate = str.substring(1, 12);
                }
                if (strLine.length() < 10) continue;
                String strPin = strLine.substring(1, 9);
                if (strPin.matches("[0-9]*")){
                    String strNF = strLine.substring(11, 34);
                    pos = strNF.indexOf(" ");
                    String strName = strNF.substring(0, pos).trim();
                    String strFirstName = strNF.substring(pos + 1, strNF.length()).trim();
                    String strCountry = strLine.substring(49, 52).trim();
                    String strClub = strLine.substring(53, 57).trim();
                    int rating = new Integer(strLine.substring(71, 75).trim()).intValue();
                    RatedPlayer rP = new RatedPlayer(
                            strPin, "", "", strName, strFirstName, strCountry, strClub, rating, "EGF");
                    vPlayers.add(rP);
                }
            }

            if (ratingListType == TYPE_FFG){
                pos = strLine.indexOf("Echelle au ");
                if (pos >= 0) {
                    strPublicationDate = strLine.substring(pos + 11, strLine.length());
                }
                if (strLine.length() < 45){
//                    JOptionPane.showMessageDialog(null, strLine);
                    continue;
                }
                String strNF = strLine.substring(0, 25);
                if (strNF.matches("[a-zA-Z].*")){
                    pos = strNF.indexOf(" ");
                    String strName = strNF.substring(0, pos).trim();
                    String strFirstName = strNF.substring(pos + 1, strNF.length()).trim();
                    int rating = new Integer(strLine.substring(25, 30).trim()).intValue();
                    String strFfgLicenceStatus = strLine.substring(31, 32); 
                    String strFfgLicence = strLine.substring(33, 40);
                    String strCC = strLine.substring(41, 45).trim();
                    String strCountry;
                    String strClub;
                    if (strCC.length() <= 2){
                        strCountry = strCC;
                        strClub = "";
                    }
                    else{
                        strCountry = "";
                        strClub = strCC;
                    }
                 
                    String strClubCountry = strLine.substring(31, 35).trim();
                    RatedPlayer rP = new RatedPlayer(
                            "", strFfgLicence, strFfgLicenceStatus, strName, strFirstName, strCountry, strClub, rating, "FFG");
                    vPlayers.add(rP);
                }
            }
        }            
    }

    public Vector<RatedPlayer> getVPlayers() {
        return vPlayers;
    }

    public String getStrPublicationDate() {
        return strPublicationDate;
    }

    public int getRatingListType() {
        return ratingListType;
    }

    public void setRatingListType(int ratingListType) {
        this.ratingListType = ratingListType;
    }
}


